home *** CD-ROM | disk | FTP | other *** search
/ MacAddict 108 / MacAddict108.iso / Software / Internet & Communication / WordPress 1.5.1.dmg / wordpress / wp-admin / import-rss.php < prev    next >
Encoding:
PHP Script  |  2005-04-19  |  6.6 KB  |  191 lines

  1. <?php
  2. define('RSSFILE', '');
  3. // Example:
  4. // define('RSSFILE', '/home/example/public_html/rss.xml');
  5. // or if it's in the same directory as import-rss.php
  6. // define('RSSFILE', 'rss.xml');
  7.  
  8. $post_author = 1; // Author to import posts as author ID
  9. $timezone_offset = 0; // GMT offset of posts your importing
  10.  
  11. function unhtmlentities($string) { // From php.net for < 4.3 compat
  12.    $trans_tbl = get_html_translation_table(HTML_ENTITIES);
  13.    $trans_tbl = array_flip($trans_tbl);
  14.    return strtr($string, $trans_tbl);
  15. }
  16.  
  17. $add_hours = intval($timezone_offset);
  18. $add_minutes = intval(60 * ($timezone_offset - $add_hours));
  19.  
  20. if (!file_exists('../wp-config.php')) die("There doesn't seem to be a wp-config.php file. You must install WordPress before you import any entries.");
  21. require('../wp-config.php');
  22.  
  23. $step = $_GET['step'];
  24. if (!$step) $step = 0;
  25. header( 'Content-Type: text/html; charset=utf-8' );
  26. ?>
  27. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  28. <html xmlns="http://www.w3.org/1999/xhtml">
  29. <title>WordPress › Import from RSS</title>
  30. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  31. <style media="screen" type="text/css">
  32.     body {
  33.         font-family: Georgia, "Times New Roman", Times, serif;
  34.         margin-left: 20%;
  35.         margin-right: 20%;
  36.     }
  37.     #logo {
  38.         margin: 0;
  39.         padding: 0;
  40.         background-image: url(http://wordpress.org/images/logo.png);
  41.         background-repeat: no-repeat;
  42.         height: 60px;
  43.         border-bottom: 4px solid #333;
  44.     }
  45.     #logo a {
  46.         display: block;
  47.         text-decoration: none;
  48.         text-indent: -100em;
  49.         height: 60px;
  50.     }
  51.     p {
  52.         line-height: 140%;
  53.     }
  54.     </style>
  55. </head><body> 
  56. <h1 id="logo"><a href="http://wordpress.org/">WordPress</a></h1> 
  57. <?php
  58. switch($step) {
  59.  
  60.     case 0:
  61. ?> 
  62. <p>Howdy! This importer allows you to extract posts from any RSS 2.0 file into your blog. This is useful if you want to import your posts from a system that is not handled by a custom import tool. To get started you must edit the following line in this file (<code>import-rss.php</code>) </p>
  63. <p><code>define('RSSFILE', '');</code></p>
  64. <p>You want to define where the RSS file we'll be working with is, for example: </p>
  65. <p><code>define('RSSFILE', 'rss.xml');</code></p>
  66. <p>You have to do this manually for security reasons. When you're done reload this page and we'll take you to the next step.</p>
  67. <?php if ('' != RSSFILE) : ?>
  68. <h2 style="text-align: right;"><a href="import-rss.php?step=1">Begin RSS Import »</a></h2>
  69. <?php endif; ?>
  70. <?php
  71.     break;
  72.  
  73.     case 1:
  74.  
  75. // Bring in the data
  76. set_magic_quotes_runtime(0);
  77. $datalines = file(RSSFILE); // Read the file into an array
  78. $importdata = implode('', $datalines); // squish it
  79. $importdata = str_replace(array("\r\n", "\r"), "\n", $importdata);
  80.  
  81. preg_match_all('|<item>(.*?)</item>|is', $importdata, $posts);
  82. $posts = $posts[1];
  83.  
  84. echo '<ol>';
  85. foreach ($posts as $post) :
  86. $title = $date = $categories = $content = $post_id =  '';
  87. echo "<li>Importing post... ";
  88.  
  89. preg_match('|<title>(.*?)</title>|is', $post, $title);
  90. $title = addslashes( trim($title[1]) );
  91. $post_name = sanitize_title($title);
  92.  
  93. preg_match('|<pubdate>(.*?)</pubdate>|is', $post, $date);
  94.  
  95. if ($date) :
  96.     $date = strtotime($date[1]);
  97. else : // if we don't already have something from pubDate
  98.     preg_match('|<dc:date>(.*?)</dc:date>|is', $post, $date);
  99.     $date = preg_replace('|([-+])([0-9]+):([0-9]+)$|', '\1\2\3', $date[1]);
  100.     $date = str_replace('T', ' ', $date);
  101.     $date = strtotime($date);
  102. endif;
  103.  
  104. $post_date = gmdate('Y-m-d H:i:s', $date);
  105.  
  106. preg_match_all('|<category>(.*?)</category>|is', $post, $categories);
  107. $categories = $categories[1];
  108.  
  109. if (!$categories) :
  110.     preg_match_all('|<dc:subject>(.*?)</dc:subject>|is', $post, $categories);
  111.     $categories = $categories[1];
  112. endif;
  113.  
  114. preg_match('|<guid.+?>(.*?)</guid>|is', $post, $guid);
  115. if ($guid) $guid = addslashes( trim($guid[1]) );
  116. else $guid = '';
  117.  
  118. preg_match('|<content:encoded>(.*?)</content:encoded>|is', $post, $content);
  119. $content = str_replace( array('<![CDATA[', ']]>'), '', addslashes( trim($content[1]) ) );
  120.  
  121. if (!$content) : // This is for feeds that put content in description
  122.     preg_match('|<description>(.*?)</description>|is', $post, $content);
  123.     $content = $wpdb->escape( unhtmlentities( trim($content[1]) ) );
  124. endif;
  125.  
  126. // Clean up content
  127. $content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $content);
  128. $content = str_replace('<br>', '<br />', $content);
  129. $content = str_replace('<hr>', '<hr />', $content);
  130.  
  131. // This can mess up on posts with no titles, but checking content is much slower
  132. // So we do it as a last resort
  133. if ('' == $title) : 
  134.     $dupe = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_content = '$content' AND post_date = '$post_date'");
  135. else :
  136.     $dupe = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_title = '$title' AND post_date = '$post_date'");
  137. endif;
  138.  
  139. // Now lets put it in the DB
  140. if ($dupe) :
  141.     echo 'Post already imported';
  142. else : 
  143.     
  144.     $wpdb->query("INSERT INTO $wpdb->posts 
  145.         (post_author, post_date, post_date_gmt, post_content, post_title,post_status, comment_status, ping_status, post_name, guid)
  146.         VALUES 
  147.         ('$post_author', '$post_date', DATE_ADD('$post_date', INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE), '$content', '$title', 'publish', '$comment_status', '$ping_status', '$post_name', '$guid')");
  148.     $post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_title = '$title' AND post_date = '$post_date'");
  149.     if (!$post_id) die("couldn't get post ID");
  150.     if (0 != count($categories)) :
  151.         foreach ($categories as $post_category) :
  152.         $post_category = unhtmlentities($post_category);
  153.         // See if the category exists yet
  154.         $cat_id = $wpdb->get_var("SELECT cat_ID from $wpdb->categories WHERE cat_name = '$post_category'");
  155.         if (!$cat_id && '' != trim($post_category)) {
  156.             $cat_nicename = sanitize_title($post_category);
  157.             $wpdb->query("INSERT INTO $wpdb->categories (cat_name, category_nicename) VALUES ('$post_category', '$cat_nicename')");
  158.             $cat_id = $wpdb->get_var("SELECT cat_ID from $wpdb->categories WHERE cat_name = '$post_category'");
  159.         }
  160.         if ('' == trim($post_category)) $cat_id = 1;
  161.         // Double check it's not there already
  162.         $exists = $wpdb->get_row("SELECT * FROM $wpdb->post2cat WHERE post_id = $post_id AND category_id = $cat_id");
  163.     
  164.          if (!$exists) { 
  165.             $wpdb->query("
  166.             INSERT INTO $wpdb->post2cat
  167.             (post_id, category_id)
  168.             VALUES
  169.             ($post_id, $cat_id)
  170.             ");
  171.             }
  172.     endforeach;
  173.     else:
  174.         $exists = $wpdb->get_row("SELECT * FROM $wpdb->post2cat WHERE post_id = $post_id AND category_id = 1");
  175.         if (!$exists) $wpdb->query("INSERT INTO $wpdb->post2cat (post_id, category_id) VALUES ($post_id, 1) ");
  176.     endif;
  177.     echo 'Done!</li>';
  178. endif;
  179.  
  180.  
  181. endforeach;
  182. ?>
  183. </ol>
  184.  
  185. <h3>All done. <a href="../">Have fun!</a></h3>
  186. <?php
  187.     break;
  188. }
  189. ?> 
  190. </body>
  191. </html>